package o2o.web.actions.order.other;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.JYMathDoubleUtils;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.order.OrderGoodsMemory;
import o2o.cache.order.OrderInfoMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.order.OrderGoods;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.order.OrderGoodsVO;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//订单商品统计
@Controller
public class OrderGoodsListAction {

    //获取时间段内  商家进行中订单商品统计信息
    @RequestMapping(method = RequestMethod.POST, value = "/shop/order/goods/list")
    @ResponseBody
    public String order_shop_list(String token,
                                  Long shopId,
                                  Long startTime, Long endTime) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证用户是否有权限
        if (!User.hasShopAuth(user.getUsername(),user.getUserType())) {
            return AjaxResult.errorResult("您没有店铺操作权限");
        }

        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("店铺ID不能为空");
        }

        if (ValidateUtil.isNotAbsLong(startTime) || ValidateUtil.isNotAbsLong(endTime)) {
            return AjaxResult.errorResult("开始时间和结束时间不能为空");
        }

        ShopInfo shopInfo = ShopMemory.getInstance().getShopById(shopId);
        if (null == shopInfo) {
            return AjaxResult.errorResult("获取店铺信息失败，请稍后再试");
        }


        List<String> list = OrderInfoMemory.getInstance().listOrderNoOfShop(shopId, startTime, endTime);
        if (!ValidateUtil.isAbsList(list)) {
            return AjaxResult.successResult(0, null);
        }

        //汇总map信息  <订单商品Id，订单商品详情>
        ConcurrentHashMap<Long, OrderGoodsVO> map = new ConcurrentHashMap<>();
        //汇总map信息  <订单多规格商品Id，订单多规格商品详情>
        ConcurrentHashMap<Long, OrderGoodsVO> specMap = new ConcurrentHashMap<>();
        for (int i = 0; i < list.size(); i++) {
            if (ValidateUtil.isNull(list.get(i))) {
                continue;
            }

            List<OrderGoods> goodsList = OrderGoodsMemory.getInstance().listOfOrderNo(list.get(i));
            if (ValidateUtil.isAbsList(goodsList)) {
                for (int j = 0; j < goodsList.size(); j++) {
                    if (null == goodsList.get(j)) {
                        continue;
                    }
                    OrderGoodsVO infoVO = new OrderGoodsVO();
                    try {
                        ObjectUtils.copyPropertiesExclude(goodsList.get(j), infoVO, null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    OrderGoodsVO mapGoods = null;
                    if (ValidateUtil.isNotID(infoVO.getGoodsSpecsId())) {
                        //非多规格
                        if (map.containsKey(infoVO.getId())) {
                            mapGoods = map.get(infoVO.getId());
                        }
                    } else {
                        if (specMap.containsKey(infoVO.getId())) {
                            mapGoods = specMap.get(infoVO.getId());
                        }
                    }
                    if (null != mapGoods) {
                        Integer totalCount = mapGoods.getTotalCount(); //总个数
                        totalCount = totalCount + infoVO.getTotalCount();
                        infoVO.setTotalCount(totalCount);

                        Double totalPrice = mapGoods.getTotalPrice(); //总金额
                        totalPrice = JYMathDoubleUtils.add(totalPrice, infoVO.getTotalCount());
                        infoVO.setTotalPrice(totalPrice);

                        Double totalPacketPrice = mapGoods.getTotalPacketPrice(); //餐盒费总价
                        totalPacketPrice = JYMathDoubleUtils.add(totalPacketPrice, infoVO.getTotalPacketPrice());
                        infoVO.setTotalPacketPrice(totalPacketPrice);

                        Double totalMoney = mapGoods.getTotalMoney(); //含打包费总价
                        totalMoney = JYMathDoubleUtils.add(totalMoney, infoVO.getTotalMoney());
                        infoVO.setTotalMoney(totalMoney);
                    }

                    if (ValidateUtil.isNotID(infoVO.getGoodsSpecsId())) {
                        //非多规格
                        map.put(infoVO.getId(), infoVO);
                    } else {
                        specMap.put(infoVO.getId(), infoVO);
                    }


                    //                    if (map.containsKey(infoVO.getId())) {
//                        mapGoods = map.get(infoVO.getId());
//                        if (null == mapGoods) {
//                            continue;
//                        }
//                        Integer totalCount = mapGoods.getTotalCount(); //总个数
//                        totalCount = totalCount + infoVO.getTotalCount();
//                        infoVO.setTotalCount(totalCount);
//
//                        Double totalPrice = mapGoods.getTotalPrice(); //总金额
//                        totalPrice = JYMathDoubleUtils.add(totalPrice, infoVO.getTotalCount());
//                        infoVO.setTotalPrice(totalPrice);
//
//                        Double totalPacketPrice = mapGoods.getTotalPacketPrice(); //餐盒费总价
//                        totalPacketPrice = JYMathDoubleUtils.add(totalPacketPrice, infoVO.getTotalPacketPrice());
//                        infoVO.setTotalPacketPrice(totalPacketPrice);
//
//                        Double totalMoney = mapGoods.getTotalMoney(); //含打包费总价
//                        totalMoney = JYMathDoubleUtils.add(totalMoney, infoVO.getTotalMoney());
//                        infoVO.setTotalMoney(totalMoney);
//                    } else {
//                        map.put(infoVO.getId(), infoVO);
//                    }

                }
            }
        }


        List<OrderGoodsVO> voList = new ArrayList<>();
        if (null != map && map.size() > 0) {
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                if (null == entry) {
                    continue;
                }
                if (null == entry.getValue()) {
                    continue;
                }
                OrderGoodsVO vo = (OrderGoodsVO) entry.getValue();
                if (null == vo) {
                    continue;
                }
                voList.add(vo);
            }
        }

        return AjaxResult.successResult(voList.size(), voList);
    }

}
